package jorm;

import java.sql.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @auther:陈磊 <br/>
 * Date: 12-12-29<br/>
 * Time: 下午4:59<br/>
 * connectMethod:13638363871@163.com<br/>
 *  ConnectinProvider
 * common  Operations Tool box
 * get more ,please see OrmProvider.Orm.Db
 */
public class ConnectinProvider implements IOperations {

     private static final ConnectinProvider COMMON_OPERATIONS=new ConnectinProvider();

     private ConnectinProvider(){}

    public static ConnectinProvider getCommonOperations(){
        return COMMON_OPERATIONS;
    }
    /**
     * 获取连接
     *
     * @return
     */
    @Override
    public Connection getConnection(){
        try {
            return DbKit.getConnection();  //To change body of implemented methods use File | Settings | File Templates.
        } catch (SQLException e) {
            Logger.getLogger(ConnectinProvider.class.getName()).log(Level.WARNING, "getConnection error... ...", e);
            return null;
        }
    }

    /**
     * 关闭连接
     *
     * @return
     */
    @Override
    public void close(Connection connection) {
        try {
            if (!connection.isClosed())
                DbKit.close(connection);
        } catch (SQLException e) {
            Logger.getLogger(ConnectinProvider.class.getName()).log(Level.WARNING, "connection error... ...", e);
        }
    }

    /**
     * 关闭连接
     * @param st   Statement
     * @param conn   Connection
     */
    @Override
    public void close(Statement st, Connection conn) {
        DbKit.close(st, conn);
    }

    /**
     *   关闭连接
     * @param rs   ResultSet
     * @param st  Statement
     * @param conn   Connection
     */
    @Override
    public void close(ResultSet rs, Statement st, Connection conn) {
        DbKit.close(rs, st, conn);
    }

    /**
     * @param sql
     * @return
     */
    @Override
    public int update(String sql) {
        return Db.update(sql);  //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * update
     *
     * @param conn
     * @param sql
     * @param paras
     * @return either (1) the row count for SQL Data Manipulation Language (DML) statements
     *         or (2) 0 for SQL statements that return nothing
     * @throws java.sql.SQLException
     */
    @Override
    public int update(Connection conn, String sql, Object... paras) {
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            fillPreparedStatement(pst,paras);
            return pst.executeUpdate();
        } catch (SQLException e) {
            Logger.getLogger(ConnectinProvider.class.getName()).log(Level.WARNING, e.getMessage());   //To change body of catch statement use File | Settings | File Templates.
            return 0;
        }

    }

    /**
     * Delete record by id.
     * Example: boolean succeed = Db.deleteById("user", "user_id", 15);
     *
     * @param tableName  the table name of the table
     * @param primaryKey the primary key of the table
     * @param id         the id value of the record
     * @return true if delete succeed otherwise false
     */
    @Override
    public boolean deleteById(String tableName, String primaryKey, Object id) {
        return Db.deleteById(tableName, primaryKey, id);  //To change body of implemented methods use File | Settings | File Templates.
    }
    /**
     * delete
     *
     * @param conn
     * @param sql
     * @param paras
     * @return <code>true</code> if the first result is a <code>ResultSet</code>
     *         object; <code>false</code> if the first result is an update
     *         count or there is no result
     */
    @Override
    public boolean delete(Connection conn, String sql, Object... paras)  {
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            fillPreparedStatement(pst,paras);
            return pst.execute();
        } catch (SQLException e) {
            Logger.getLogger(ConnectinProvider.class.getName()).log(Level.WARNING, e.getMessage());   //To change body of catch statement use File | Settings | File Templates.
            return false;
        }

    }

    /**
     *
     * @param conn
     * @param sql
     * @param paras
     */
    @Override
    public void insert(Connection conn, String sql, Object... paras) throws SQLException {
        PreparedStatement pst = conn.prepareStatement(sql);
        fillPreparedStatement(pst,paras);
        pst.executeUpdate();

        this.close(pst,conn);
    }
    @Override
    public void fillPreparedStatement(PreparedStatement pst,Object... paras){
         for (int i=0; i<paras.length; i++) {
             try {
                 pst.setObject(i + 1, paras[i]);
             } catch (SQLException e) {
                 Logger.getLogger(ConnectinProvider.class.getName()).log(Level.WARNING,e.getMessage()); //To change body of catch statement use File | Settings | File Templates.
             }
         }
     }

    /**
     *
     * */
    @Override
    public List<Record> select(String sql) {
        return Db.find(sql);  //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public List<Record> select(String sql, Object... paras){
        return Db.find(sql, paras);
    }
}
